import logging

from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect

from clustermanager.lib.base import BaseController, render

import random
import string
import base64
import hashlib

log = logging.getLogger(__name__)

def gen(len=8, num_flag=True, low_flag=True, up_flag=True, special_flag=True):
        num = "0123456789"
        lower = "abcdefghijklmnopqrstuvwxyz"
        upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        special = "~!@#$%^&*()[]{}_=+-"
        
        str = ''
        if num_flag:
            str += num
        if low_flag:
            str += lower
        if up_flag:
            str += upper
#        if special_flag:
#            str += special
        if str == '':
            str = num + lower
        population = str
        
        return string.join(random.sample(str, len)).replace(" ", "")

class CipherController(BaseController):

    def index(self):
	
        # Return a rendered template
        return render('/cipher/password.mako')
        # or, return a response

    def one(self):
        length=20
        seedlower=string.lowercase
        seeddigit=string.digits
        seedupper=string.uppercase
        pwd=pwdd=pwdl=pwdu=''
        
        countl=random.randrange(1,length-1)
        countu=random.randrange(1,length-countl)
        countd=(length-countl-countu)
        
        for l in random.sample(seedlower,countl):
            pwdl+=l
        for u in random.sample(seedupper,countu):
            pwdu+=u
        for d in random.sample(seeddigit,countd):
            pwdd+=d
        
        seed=pwdl+pwdu+pwdd
        shuffler=random.sample(seed,len(seed))
        pwd="".join(shuffler)
  
        return pwd
    def batch(self):
        length = int(request.params.get('length'))
      
        #return string.join(random.sample("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length)).replace(" ", "")
        i = 0
        c.passwd=''
        while(i < 50):
            c.passwd += gen(length) + "\r\n"
            i += 1
        return render('/cipher/password.mako')
    def base64(self):
        ciphertext = request.params.get('ciphertext')
        submit = request.params.get('submit')
        try:
            if submit == 'Encode':
                c.ciphertext = base64.encodestring(ciphertext)
            if submit == 'Decode':
                c.ciphertext = base64.decodestring(ciphertext)
        except UnicodeDecodeError:
            c.ciphertext = ''
        except:
            c.ciphertext = ''
        return render('/cipher/base64.mako')
    def md5(self):
        ciphertext = request.params.get('ciphertext')
        submit = request.params.get('submit')
        try:
            c.ciphertext = md5.new(ciphertext).hexdigest()
        #except UnicodeDecodeError:
        #    sys.exit()
        #import hashlib
        #m = hashlib.md5('123456')
        #m.digest()
        #m.hexdigest()
        except:
            c.ciphertext = ''
        return render('/cipher/md5.mako')
    def sha1(self):
        ciphertext = request.params.get('ciphertext')
        submit = request.params.get('submit')
        try:
            c.ciphertext = sha.new(ciphertext).hexdigest()
        except:
            c.ciphertext = ''
        return render('/cipher/sha1.mako')

        
